<template>
    <h3>公告添加</h3>
    <div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>标题</td>
                    <td><input type="text" v-model="a.Title" maxlength="20"></td>
                </tr>
                <tr>
                    <td>公告内容</td>
                    <td><textarea v-model="a.Desc" maxlength="50"></textarea></td>
                </tr>
                <tr>
                    <td>公告图片</td>
                    <td><input type="file" @change="Imgs">
                        <img :src="a.Photo" width="200">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td><input type="radio" v-model="a.State" :value="1" name="state">显示
                        <input type="radio" v-model="a.State" :value="2" name="state">隐藏
                    </td>
                </tr>
                <tr>
                    <td>类型</td>
                    <td><select v-model="a.TellType">
                            <option value="">请选择</option>
                            <option value="服务协议">服务协议</option>
                            <option value="雇佣协议">雇佣协议</option>
                            <option value="政策协议">政策协议</option>
                        </select></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="确定" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import moment from 'moment';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

let a: any = reactive({
    Title: '',
    Desc: '',
    Photo: '',
    State: '',
    TellType: '',
})

const Imgs: any = (e: any) => {
    let obj = e.target.files[0]
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: '/api/Img/UpImg',
        method: 'post',
        data: f
    })
        .then((res) => {
            a.Photo = res.data
        })

}

const ok: any = () => {
    if (a.Title == "") {
        alert("标题不能为空");
        return;
    }
    let f = /^[\u4e00-\u9fa5]{1,20}$/
    if (!f.test(a.Title)) {
        alert("标题必须为20字以内的汉字");
        return;
    }
    if (a.State == "") {
        alert("状态不能为空");
        return;
    }
    if (a.TellType == "") {
        alert("类型不能为空");
        return;
    }

    axios({
        url: '/api/ALL/AddTell',
        method: 'post',
        data: a
    })
        .then((res) => {
            if (res.data > 0) {
                alert("添加成功");
                router.push("/tellshow");
            }
        })

}





</script>

<style scoped></style>